package com.ruoyi.ma.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.annotation.Translation;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.ma.domain.MaMaterialPlan;
import com.ruoyi.ma.domain.vo.MaMaterialPlanDataDetailsReturnVo;
import com.ruoyi.ma.domain.vo.MaMaterialPlanVo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import com.ruoyi.ma.domain.bo.MaMaterialPlanDetailsBo;
import com.ruoyi.ma.domain.vo.MaMaterialPlanDetailsVo;
import com.ruoyi.ma.domain.MaMaterialPlanDetails;
import com.ruoyi.ma.mapper.MaMaterialPlanDetailsMapper;
import com.ruoyi.ma.service.IMaMaterialPlanDetailsService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 材料计划详情Service业务层处理
 *
 * @author llb
 * @date 2024-11-01
 */
@RequiredArgsConstructor
@Service
public class MaMaterialPlanDetailsServiceImpl implements IMaMaterialPlanDetailsService {

    private final MaMaterialPlanDetailsMapper baseMapper;

    /**
     * 查询材料计划详情
     */
    @Override
    public MaMaterialPlanDetailsVo queryById(Long planDetailsId){
        return baseMapper.selectVoById(planDetailsId);
    }

    /**
     * 查询材料计划详情列表
     */
    @Override
    public TableDataInfo<MaMaterialPlanDetailsVo> queryPageList(MaMaterialPlanDetailsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MaMaterialPlanDetails> lqw = buildQueryWrapper(bo);
        Page<MaMaterialPlanDetailsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询材料计划详情列表
     */
    @Override
    public List<MaMaterialPlanDetailsVo> queryList(MaMaterialPlanDetailsBo bo) {
        LambdaQueryWrapper<MaMaterialPlanDetails> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MaMaterialPlanDetails> buildQueryWrapper(MaMaterialPlanDetailsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MaMaterialPlanDetails> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPlanId() != null, MaMaterialPlanDetails::getPlanId, bo.getPlanId());
        lqw.eq(bo.getBdId() != null, MaMaterialPlanDetails::getBdId, bo.getBdId());
        lqw.eq(bo.getGysId() != null, MaMaterialPlanDetails::getGysId, bo.getGysId());
        lqw.eq(bo.getType() != null, MaMaterialPlanDetails::getType, bo.getType());
        lqw.eq(bo.getManifestId() != null, MaMaterialPlanDetails::getManifestId, bo.getManifestId());
        lqw.eq(StringUtils.isNotBlank(bo.getManifestCode()), MaMaterialPlanDetails::getManifestCode, bo.getManifestCode());
        lqw.like(StringUtils.isNotBlank(bo.getManifestName()), MaMaterialPlanDetails::getManifestName, bo.getManifestName());
        lqw.eq(StringUtils.isNotBlank(bo.getModel()), MaMaterialPlanDetails::getModel, bo.getModel());
        lqw.eq(StringUtils.isNotBlank(bo.getSpecification()), MaMaterialPlanDetails::getSpecification, bo.getSpecification());
        lqw.eq(StringUtils.isNotBlank(bo.getUnit()), MaMaterialPlanDetails::getUnit, bo.getUnit());
        lqw.eq(bo.getMonthPurchaseNumber() != null, MaMaterialPlanDetails::getMonthPurchaseNumber, bo.getMonthPurchaseNumber());
        lqw.eq(bo.getTotalPurchaseNumber() != null, MaMaterialPlanDetails::getTotalPurchaseNumber, bo.getTotalPurchaseNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getPersonnel()), MaMaterialPlanDetails::getPersonnel, bo.getPersonnel());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), MaMaterialPlanDetails::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getBysqgyslxhd()), MaMaterialPlanDetails::getBysqgyslxhd, bo.getBysqgyslxhd());
        lqw.eq(bo.getBelongDate() != null, MaMaterialPlanDetails::getBelongDate, bo.getBelongDate());
        return lqw;
    }

    /**
     * 新增材料计划详情
     */
    @Override
    public Boolean insertByBo(MaMaterialPlanDetailsBo bo) {
        MaMaterialPlanDetails add = BeanUtil.toBean(bo, MaMaterialPlanDetails.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPlanDetailsId(add.getPlanDetailsId());
        }
        return flag;
    }

    /**
     * 修改材料计划详情
     */
    @Override
    public Boolean updateByBo(MaMaterialPlanDetailsBo bo) {
        MaMaterialPlanDetails update = BeanUtil.toBean(bo, MaMaterialPlanDetails.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MaMaterialPlanDetails entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除材料计划详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 保存材料计划详情
     *
     * @param bo
     * @return
     */
    @Override
    @Transactional
    public boolean updateDetails(MaMaterialPlanDataDetailsReturnVo bo) {
        MaMaterialPlanVo infoData = bo.getInfoData(); // 主表
        List<MaMaterialPlanDetailsVo> detailList = bo.getDetailList(); // 子表
        // 更新子表
        // 根据主表id获取子表历史
        LambdaQueryWrapper<MaMaterialPlanDetails> lqw = Wrappers.lambdaQuery();
        lqw.eq(infoData.getPlanId() != null, MaMaterialPlanDetails::getPlanId, infoData.getPlanId());
        List<MaMaterialPlanDetailsVo> hisDetailList = baseMapper.selectVoList(lqw);
        // 删除子表历史记录
        if (CollUtil.isNotEmpty(hisDetailList)) {
            List<Long> ids = hisDetailList.stream().map(MaMaterialPlanDetailsVo::getPlanDetailsId).collect(Collectors.toList());
            baseMapper.removeHis(ids); // 物理删除
        }
        // 批量添加子表记录
        List<MaMaterialPlanDetails> entityList = new ArrayList<>();
        detailList.forEach(vo -> {
            vo.setBelongDate(infoData.getBelongDate());
            MaMaterialPlanDetails entity = BeanUtil.copyProperties(vo, MaMaterialPlanDetails.class);
            entityList.add(entity);
        });
        return baseMapper.insertBatch(entityList);
    }
}
